package com.ny.dao.impl;

import com.ny.dao.ManagerDao;
import com.ny.entity.Manager;
import com.ny.until.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ManagerDaoImpl implements ManagerDao {
    //实现ManagerDao接口
    private DbUtil dbUtil = new DbUtil();//创建数据库连接
    @Override
    public Manager login(String username, String password) throws Exception {
        Connection connection = dbUtil.getCon();//1.获取数据库链接
        String sql ="select * from manager where name =? and password =?";//2.创建SQL语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);//使用预编译语句防止SQL注入
        //3.执行查询
        preparedStatement.setString(1, username);//执行SQL语句
        preparedStatement.setString(2, password);//执行SQL语句
        ResultSet resultSet = preparedStatement.executeQuery();//获取结果集

        //4.处理结果集
        if (resultSet.next()) {
            Manager manager = new Manager();
            manager.setManagerId(resultSet.getInt("manager_id"));//设置属性
            manager.setName(resultSet.getString("name"));//设置属性
            manager.setPassword(resultSet.getString("password"));//设置属性
            return manager;//返回结果
        }
        return null;//返回结果
    }
    //获取数据库链接
}
